package demiUser;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpSession;

import org.apache.commons.codec.binary.Hex;

import dataBase.DBManager;
public class DemiUser {
	DBManager dbManager;
	HttpSession session;
	protected MessageDigest md5;
	
	public  DemiUser(){
		dbManager=DBManager.getDbm();
		
		try {
			md5=MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void initDemiUser(HttpSession session){
		this.session=session;
	}
	public void insertUser(String cUserId, String cUserName, String cUserAdress, String cUserTel, String cUserAdNum, String cUserPass, int cUserGrade){
		dbManager.excuteUpdate("INSERT INTO immortal.tuser(cUserId, cUserName, cUserAdress, cUserTel, cUserAdNum, cUserPass, cUserGrade) " +
				"VALUES ('"+cUserId+"', '"+cUserName+"', '"+cUserAdress+"', '"+cUserTel+"', '"+cUserAdNum+"', '"+cUserPass+"',"+ cUserGrade+")");
	
	}
	public void deleteUser(String cUserId){
		dbManager.excuteUpdate("delete immortal.tuser where cUerId ='"+cUserId+"'");

	}
	public void updateUser(String cUserId,String cUserName, String cUserAdress, String cUserTel, String cUserAdNum, String cUserPass, int cUserGrade){
		dbManager.excuteUpdate("UPDATE immortal.tuser SET  cUserName = 'cUserName', " +
				"cUserAdress = '"+cUserAdress+"', " +
				"cUserTel = '"+cUserTel+"', " +
				"cUserAdNum = '"+cUserAdNum+"', " +
				"cUserPass = '"+cUserPass+"', " +
				"cUserGrade = "+cUserGrade +
				" WHERE cUserId = '"+cUserId+"' )");
	}
	/*
	 * MD5로 변환
	 */
	public String toMD5(String code){
		StringBuffer stringbuffer=new StringBuffer();
		String vReturn;
		byte[] tempCode=md5.digest(code.getBytes());
		for(int i=0;i<tempCode.length;i++){
			stringbuffer.append(String.format("%2x",0xff &tempCode[i]));
		}
		vReturn=(stringbuffer.toString());
		vReturn=vReturn.replace(' ', '0');
		return vReturn;
	}
	
	/*
	 * 회원 리스트 ArrayList <HashMap>으로 
	 */
	public ArrayList getList(){
		ArrayList returnList=null;
		returnList =dbManager.getQueryDataArayList("select * from tUser");
		return returnList;
	}
	/*
	 * 아이디가 존제하는 여부 있으면 트루
	 */
	public boolean isThereId(String userId)
	{
		ArrayList tempList=null;
		tempList =dbManager.getQueryDataArayList("select cUserId from tUser where cUserId ='"+userId+"'");
		System.out.println(tempList.size());
		if(tempList.size()==0){
			return false;
		}
		else{
			return true;
		}
	}
	/*
	 * 가입 함수
	 * 그래이드에 1을 기본으로 넣는다 일반 회원
	 */
	public void doJoin(String cUserId,String cUserName,String cUserPass){
		System.out.println(cUserName);
		
		dbManager.excuteUpdate("INSERT INTO immortal.tuser(cUserId, cUserName, cUserPass, cUserGrade) " +
				"VALUES ('"+cUserId+"', '"+cUserName+"', md5('"+cUserPass+"'),"+ 1+")");
		
	}
	/*
	 * 로그인 상태를 얻다
	 * grade
	 * =0비로그인
	 * =1일반회원로그인
	 * =999관리자 로그인
	 */
	public int getGrade(){
		int grade=0;
		String userId=(String)session.getAttribute("userId");
		if(userId!=null){
			grade =Integer.parseInt((String)session.getAttribute("grade"));
		}
		else{
			grade=0;
		}
		return grade;
	}
	/*
	 * 로그인
	 * 0=성공
	 * 1=패스워드 틀림
	 * 2=아이디없음
	 */
	public int userLogin(String userId,String password){
		int result=0;
		ArrayList tempList=null;
		HashMap tempMap;
		String tempPass;
		String tempGrade;
		tempList =dbManager.getQueryDataArayList("select cUserId,cUserPass,cUserGrade from tUser where cUserId ='"+userId+"'");
		password=toMD5(password);
		
		/*아이디가 있음*/
		if(tempList.size()>0)
		{
			tempMap=(HashMap)tempList.get(0);
			tempPass=(String)tempMap.get("cUserPass");
			tempGrade=(String)tempMap.get("cUserGrade");
			/*패스워드가 맞음 로그인 성공*/
			if(password.equals(tempPass))
			{
				session.setMaxInactiveInterval(1000*60*60);/*한시간이 맞기를*/
				session.setAttribute("userId",userId);
				session.setAttribute("userGrade",tempGrade);
				result=0;
			}
			/*패스워드가 틀림*/
			else{
				result=1;
			}
		}
		/*아이디없음*/
		else{
			result=2;
		}
		return result;
		
	}
	
	
}
